package com.atguigu.lease.web.admin.service.impl;

import com.atguigu.lease.model.entity.*;
import com.atguigu.lease.model.enums.ItemType;
import com.atguigu.lease.web.admin.mapper.*;
import com.atguigu.lease.web.admin.service.*;
import com.atguigu.lease.web.admin.vo.apartment.ApartmentDetailVo;
import com.atguigu.lease.web.admin.vo.apartment.ApartmentItemVo;
import com.atguigu.lease.web.admin.vo.apartment.ApartmentQueryVo;
import com.atguigu.lease.web.admin.vo.apartment.ApartmentSubmitVo;
import com.atguigu.lease.web.admin.vo.fee.FeeValueVo;
import com.atguigu.lease.web.admin.vo.graph.GraphVo;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;
import java.util.stream.Collectors;

/**
 * @author liubo
 * @description 针对表【apartment_info(公寓信息表)】的数据库操作Service实现
 * @createDate 2023-07-24 15:48:00
 */
@Service
public class ApartmentInfoServiceImpl extends ServiceImpl<ApartmentInfoMapper, ApartmentInfo>
        implements ApartmentInfoService {

    @Autowired  // 公寓配套
    private ApartmentFacilityService apartmentFacilityService;

    @Autowired // 公寓标签
    private ApartmentLabelService apartmentLabelService;

    @Autowired // 公寓杂费
    private ApartmentFeeValueService apartmentFeeValueService;

    @Autowired // 公寓图片
    private GraphInfoService graphInfoService;

    @Autowired // 省份信息
    private ProvinceInfoMapper provinceInfoMapper;

    @Autowired // 城市信息
    private CityInfoMapper cityInfoMapper;

    @Autowired // 区域信息
    private DistrictInfoMapper districtInfoMapper;

    @Autowired
    private LabelInfoMapper labelInfoMapper;

    @Autowired
    private FacilityInfoMapper facilityInfoMapper;

    @Autowired
    private FeeValueMapper feeValueMapper;

    @Autowired
    private GraphInfoMapper graphInfoMapper;

    @Override
    public void saveOrUpdateApartmentInfo(ApartmentSubmitVo apartmentSubmitVo) {
        // 判断：apartmentSubmitVo.getId()是否存在
        Long apartmentId = apartmentSubmitVo.getId();
//        boolean isUpdate = apartmentSubmitVo.getId() != null;
        // 1. 保存公寓基本信息
       // super.saveOrUpdate(apartmentSubmitVo);
        // 简化开发流程：先删除所有关联信息，再保存
        // 2. 删除公寓相关信息
        if (apartmentId != null) {
            // 删除公寓配套信息
            LambdaQueryWrapper<ApartmentFacility> facilityLambdaQueryWrapper = new LambdaQueryWrapper<>();
            facilityLambdaQueryWrapper.eq(ApartmentFacility::getApartmentId, apartmentSubmitVo.getId());
            apartmentFacilityService.remove(facilityLambdaQueryWrapper);

            // 删除公寓标签信息
            LambdaQueryWrapper<ApartmentLabel> labelLambdaQueryWrapper = new LambdaQueryWrapper<>();
            labelLambdaQueryWrapper.eq(ApartmentLabel::getApartmentId, apartmentSubmitVo.getId());
            apartmentLabelService.remove(labelLambdaQueryWrapper);

            // 删除公寓杂费信息
            LambdaQueryWrapper<ApartmentFeeValue> feeValueLambdaQueryWrapper = new LambdaQueryWrapper<>();
            feeValueLambdaQueryWrapper.eq(ApartmentFeeValue::getApartmentId, apartmentSubmitVo.getId());
            apartmentFeeValueService.remove(feeValueLambdaQueryWrapper);

            // 删除公寓图片信息
            LambdaQueryWrapper<GraphInfo> graphInfoLambdaQueryWrapper = new LambdaQueryWrapper<>();
            // 存在公寓和房间两种枚举类型，则需判断类型是否为公寓类型
            graphInfoLambdaQueryWrapper.eq(GraphInfo::getItemType, ItemType.APARTMENT);
            graphInfoLambdaQueryWrapper.eq(GraphInfo::getItemId, apartmentSubmitVo.getId());
            graphInfoService.remove(graphInfoLambdaQueryWrapper);
        }
        Long provinceId = apartmentSubmitVo.getProvinceId();
        Long cityId = apartmentSubmitVo.getCityId();
        Long districtId = apartmentSubmitVo.getDistrictId();
        ProvinceInfo provinceInfo = provinceInfoMapper.selectById(provinceId);
        CityInfo cityInfo = cityInfoMapper.selectById(cityId);
        DistrictInfo districtInfo = districtInfoMapper.selectById(districtId);
        apartmentSubmitVo.setProvinceName(provinceInfo.getName());// 省名
        apartmentSubmitVo.setCityName(cityInfo.getName());// 城市名
        apartmentSubmitVo.setDistrictName(districtInfo.getName());// 地区名

        super.saveOrUpdate(apartmentSubmitVo);

        // 新增时候要返回主键
       // apartmentId = apartmentSubmitVo.getId();
        //  apartmentSubmitVo.getId() == 主键返回策略


        // 3. 新增（插入）公寓配套信息 根据apartmentSubmitVo新增属性
        /**
         *  private List<Long> facilityInfoIds
         *  private List<Long> labelIds
         *  private List<Long> feeValueIds
         *  private List<GraphVo> graphVoList
         */
        // 插入公寓配套信息
        // 获取配套ids
        List<Long> facilityInfoIds = apartmentSubmitVo.getFacilityInfoIds();
        // 创建配套对象集合
        ArrayList<ApartmentFacility> apartmentFacilities = new ArrayList<>();
        // 循环遍历ids
        for (Long facilityInfoId : facilityInfoIds) {
            // 创建ApartmentFacility对象
            ApartmentFacility apartmentFacility = ApartmentFacility.builder().build();
            // 设置apartmentId
            apartmentFacility.setApartmentId(apartmentSubmitVo.getId());
            // 设置facilityId
            apartmentFacility.setFacilityId(facilityInfoId);
            apartmentFacilities.add(apartmentFacility);
        }
        // 批量插入
        apartmentFacilityService.saveBatch(apartmentFacilities);


        // 插入公寓标签信息
        List<Long> labelIds = apartmentSubmitVo.getLabelIds();
        ArrayList<ApartmentLabel> apartmentLabels = new ArrayList<>();
        for (Long labelId : labelIds) {
            ApartmentLabel apartmentLabel = ApartmentLabel.builder().build();
            apartmentLabel.setApartmentId(apartmentSubmitVo.getId());
            apartmentLabel.setLabelId(labelId);
            apartmentLabels.add(apartmentLabel);
        }
        apartmentLabelService.saveBatch(apartmentLabels);

        // 插入公寓杂费信息
        List<Long> feeValueIds = apartmentSubmitVo.getFeeValueIds();
        ArrayList<ApartmentFeeValue> apartmentFeeValues = new ArrayList<>();
        for (Long feeValueId : feeValueIds) {
            ApartmentFeeValue apartmentFeeValue = ApartmentFeeValue.builder().build();
            apartmentFeeValue.setApartmentId(apartmentSubmitVo.getId());
            apartmentFeeValue.setFeeValueId(feeValueId);
            apartmentFeeValues.add(apartmentFeeValue);
        }
        apartmentFeeValueService.saveBatch(apartmentFeeValues);

        // 插入公寓图片信息
        List<GraphVo> graphVoList = apartmentSubmitVo.getGraphVoList();
        ArrayList<GraphInfo> graphInfos = new ArrayList<>();
        for (GraphVo graphVo : graphVoList) {
            GraphInfo graphInfo = new GraphInfo();
            graphInfo.setName(graphVo.getName());
            graphInfo.setUrl(graphVo.getUrl());
            graphInfo.setItemType(ItemType.APARTMENT);
            graphInfo.setItemId(apartmentSubmitVo.getId());
            graphInfos.add(graphInfo);
        }
        graphInfoService.saveBatch(graphInfos);

    }

    @Override
    public IPage<ApartmentItemVo> pageApartmentItemByQuery(IPage<ApartmentItemVo> page, ApartmentQueryVo queryVo) {
        IPage<ApartmentItemVo> list =baseMapper.pageApartmentItemByQuery(page, queryVo);
        return list;
    }

    @Override
    public void removeApartmentInfoById(Long id) {
        super.removeById(id);
        // 删除公寓配套信息
        LambdaQueryWrapper<ApartmentFacility> facilityLambdaQueryWrapper = new LambdaQueryWrapper<>();
        facilityLambdaQueryWrapper.eq(ApartmentFacility::getApartmentId, id);
        apartmentFacilityService.remove(facilityLambdaQueryWrapper);
        // 删除公寓标签信息
        LambdaQueryWrapper<ApartmentLabel> labelLambdaQueryWrapper = new LambdaQueryWrapper<>();
        labelLambdaQueryWrapper.eq(ApartmentLabel::getApartmentId, id);
        apartmentLabelService.remove(labelLambdaQueryWrapper);
        // 删除公寓杂费信息
        LambdaQueryWrapper<ApartmentFeeValue> feeValueLambdaQueryWrapper = new LambdaQueryWrapper<>();
        feeValueLambdaQueryWrapper.eq(ApartmentFeeValue::getApartmentId, id);
        apartmentFeeValueService.remove(feeValueLambdaQueryWrapper);
        // 删除公寓图片信息
        LambdaQueryWrapper<GraphInfo> graphInfoLambdaQueryWrapper = new LambdaQueryWrapper<>();
        graphInfoLambdaQueryWrapper.eq(GraphInfo::getItemType, ItemType.APARTMENT);
        graphInfoLambdaQueryWrapper.eq(GraphInfo::getItemId, id);
        graphInfoService.remove(graphInfoLambdaQueryWrapper);
    }

    @Override
    public ApartmentDetailVo listApartmentSubmitVoById(Long id) {

        ApartmentDetailVo apartmentDetailVo = new ApartmentDetailVo();
        // 获取公寓基本信息
        ApartmentInfo apartmentInfo = baseMapper.selectById(id);
        BeanUtils.copyProperties(apartmentInfo, apartmentDetailVo);

        // 获取图片列表
//        LambdaQueryWrapper<GraphInfo> graphInfoLambdaQueryWrapper = new LambdaQueryWrapper<>();
//        graphInfoLambdaQueryWrapper.eq(GraphInfo::getItemType, ItemType.APARTMENT);
//        graphInfoLambdaQueryWrapper.eq(GraphInfo::getItemId, id);
//        List<GraphInfo> graphInfos = graphInfoService.list(graphInfoLambdaQueryWrapper);
//        // 创建GraphVo对象集合
//        List<GraphVo> graphVoList = graphInfos.stream().map(graphInfo -> {
//            GraphVo graphVo = new GraphVo();
//            BeanUtils.copyProperties(graphInfo, graphVo);
//            return graphVo;
//        }).collect(Collectors.toList());
        List<GraphVo> graphVoList = graphInfoMapper.selectGraphVoByItemIdAndItemType(ItemType.APARTMENT,id);
        // 添加图片列表属性
        apartmentDetailVo.setGraphVoList(graphVoList);

        // 标签信息
        List<LabelInfo> labelInfoList = labelInfoMapper.selectListByApartmentId(id);
        apartmentDetailVo.setLabelInfoList(labelInfoList);

        // 配套信息
        List<FacilityInfo> facilityInfoList = facilityInfoMapper.selectListByApartmentId(id);
        apartmentDetailVo.setFacilityInfoList(facilityInfoList);

        // 杂费信息
        List<FeeValueVo> feeValueVos = feeValueMapper.selectListByApartmentId(id);
        apartmentDetailVo.setFeeValueVoList(feeValueVos);



        return apartmentDetailVo;
    }


}




